import { ModalTypeEnum } from '@/enums/typeEnums';
import { useMemo, useState } from 'react';

export type XcModalType = 'add' | 'edit' | 'detail' | 'backup' | 'view';

export interface XcModalState {
  defaultVisible?: boolean;
  defaultType?: XcModalType;
}

const useFormModal = ({ defaultVisible = false, defaultType = 'add' }: XcModalState) => {
  const [type, setType] = useState<XcModalType>(defaultType);
  const [visible, setVisible] = useState(defaultVisible);
  const [title, setTitle] = useState('');

  const setTypeWithVisible = (val: XcModalType) => {
    setType(val);
    setTitle(ModalTypeEnum[val]);
    setVisible(true);
  };
  return useMemo(() => {
    return {
      type,
      visible,
      title,
      setTypeWithVisible,
      setVisible,
    };
  }, [type, visible]);
};

export default useFormModal;
